package com.zhengke.controller.ddgl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhengke.pojo.AjaxResult;
import com.zhengke.pojo.Member;
import com.zhengke.pojo.SysOrderResult;
import com.zhengke.service.IMemberService;
/**
 * 会员管理的控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/member")
public class IMemberController {
	
	//注入IMemberService对象
	@Autowired
	private IMemberService imemberService;
	
	//跳转页面的方法
	@RequestMapping("/toMemberPage.do")
	public String gotoMemberMessage() {
		return "/member";
	}
	
	//查询所有的会员的方法
	@RequestMapping("/querryMember.do")
	@ResponseBody
	public SysOrderResult querryMemberMethod(String toNumber,String cityName,int page,int limit) {
		SysOrderResult sysOrderResult = null;
		System.out.println("toNumber=======>"+toNumber+"  --cityName======》"+cityName +" --AA ===============>"+page+"   --AA==============>" +limit);
		try {
			List<Member> memberList = new ArrayList<Member>();
			PageHelper.startPage(page, limit, true);
			Map<String,Object> memberMessageMap = new HashMap<String, Object>();
			//Integer cityNumberInteger = Integer.parseInt(cityName);
			//Integer totalNumberInteger = Integer.parseInt(toNumber);
			//System.out.println(cityNumberInteger);
			//System.out.println(totalNumberInteger);
			memberMessageMap.put("province_id",cityName);
			List<Map<String, Object>> memberListMap = imemberService.querryMemberList(memberMessageMap);
			System.out.println(memberListMap);
			for(Map<String, Object> map : memberListMap) {
				Member member = new Member();
				member.setUser_id(map.get("user_id")+"");
				member.setUser_name(map.get("user_name")+"");
				member.setUser_card_number(map.get("user_card_number")+"");
				member.setStatu(map.get("statu")+"");
				member.setUser_integral(map.get("user_integral")+"");
				member.setUser_balance(map.get("user_balance")+"");
				member.setCreate_time(map.get("create_time")+"");
				member.setUser_phone(map.get("user_phone")+"");
				memberList.add(member);
			}
			// 将前台返回的两个数据的参数进行接收，然后返回，如果后面的参数设置成true的话就会自动进行返回
			// 将查询到的所有的订单的结果交给PageHelper进行处理
			PageInfo<Member> pageInfo = new PageInfo<Member>(memberList);
			sysOrderResult = new SysOrderResult();
			sysOrderResult.setCode("0");
			sysOrderResult.setMsg("");
			sysOrderResult.setData(memberList);
			sysOrderResult.setCount(100L);
		}catch(Exception e) {
			e.getStackTrace();
		}
		return sysOrderResult;
	}
	
	@RequestMapping("/rechargeMethod.do")
	@ResponseBody
	public AjaxResult rechargeMethod(HttpServletRequest request,HttpServletResponse response ) {
		try {
			String memberId = request.getParameter("member_id");//会员的id
			String member_money = request.getParameter("member_money");//会员充值的金额
			String memberName = request.getParameter("member_name");//会员的姓名
			String user_id = memberId;
			//获取当前的会员的余额
			String user_balance = imemberService.querrYmemberBymemberId(user_id);
		    double oldbalance = Double.parseDouble(user_balance);
		    //获取充值的金额
		    double newblance = Double.parseDouble(member_money);
		    double toNumber = oldbalance+newblance;
		    //设置金额的后面的小数点的位数
		    DecimalFormat df = new DecimalFormat( "0.00");
		    String canNumber = df.format(toNumber);
		    Map<String,Object> messageMap = new HashMap<String,Object>();
		    messageMap.put("user_balance",canNumber);//传入订单的总金额
		    messageMap.put("user_id", memberId);//出入会员的id
		    int result =  imemberService.updateMemberMethod(messageMap);
		    if(result == 1 ) {
		    	return new AjaxResult("200","会员充值成功！");
		    }else {
		    	return new AjaxResult("300","**会员充值失败**");
		    }
		}catch(Exception e) {
			e.getStackTrace();
			return new AjaxResult("300","系统异常！！");
		}
	}
}
